<template>
  <view class="create-container">
    <view class="header">
      <text class="title">创建家庭</text>
      <text class="subtitle">建立您的数字家庭空间</text>
    </view>
    
    <view class="form-section">
      <view class="form-item">
        <text class="label">家庭名称</text>
        <input 
          class="input" 
          v-model="familyForm.name" 
          placeholder="请输入家庭名称"
          maxlength="50"
        />
      </view>
      
      <view class="form-item">
        <text class="label">家庭描述</text>
        <textarea 
          class="textarea" 
          v-model="familyForm.description" 
          placeholder="请简单描述您的家庭（可选）"
          maxlength="200"
        />
      </view>
    </view>
    
    <view class="action-section">
      <button 
        class="create-btn"
        @click="handleCreateFamily"
        :disabled="!familyForm.name || loading"
      >
        <text v-if="loading">创建中...</text>
        <text v-else>创建家庭</text>
      </button>
      
      <view class="divider">
        <text>或者</text>
      </view>
      
      <button 
        class="join-btn"
        @click="goToJoin"
      >
        加入现有家庭
      </button>
    </view>
  </view>
</template>

<script>
import { mapActions } from 'vuex'

export default {
  name: 'CreateFamily',
  data() {
    return {
      familyForm: {
        name: '',
        description: ''
      },
      loading: false
    }
  },
  
  methods: {
    ...mapActions('family', ['createFamily']),
    
    async handleCreateFamily() {
      if (!this.familyForm.name.trim()) {
        uni.showToast({
          title: '请输入家庭名称',
          icon: 'none'
        })
        return
      }
      
      this.loading = true
      
      try {
        await this.createFamily({
          name: this.familyForm.name.trim(),
          description: this.familyForm.description.trim()
        })
        
        uni.showToast({
          title: '创建成功',
          icon: 'success'
        })
        
        setTimeout(() => {
          uni.reLaunch({
            url: '/pages/index/index'
          })
        }, 1500)
        
      } catch (error) {
        console.error('创建家庭失败:', error)
        uni.showToast({
          title: error.message || '创建失败',
          icon: 'none'
        })
      } finally {
        this.loading = false
      }
    },
    
    goToJoin() {
      uni.navigateTo({
        url: '/pages/family/join'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.create-container {
  min-height: 100vh;
  padding: 40rpx;
  background-color: #f5f5f5;
}

.header {
  text-align: center;
  margin-bottom: 80rpx;
  
  .title {
    display: block;
    font-size: 48rpx;
    font-weight: bold;
    color: #333;
    margin-bottom: 16rpx;
  }
  
  .subtitle {
    font-size: 28rpx;
    color: #666;
  }
}

.form-section {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 40rpx;
  margin-bottom: 60rpx;
  
  .form-item {
    margin-bottom: 40rpx;
    
    &:last-child {
      margin-bottom: 0;
    }
    
    .label {
      display: block;
      font-size: 32rpx;
      color: #333;
      margin-bottom: 16rpx;
      font-weight: 500;
    }
    
    .input, .textarea {
      width: 100%;
      height: 80rpx;
      padding: 24rpx;
      border: 2rpx solid #eee;
      border-radius: 12rpx;
      font-size: 32rpx;
      box-sizing: border-box;
      
      &:focus {
        border-color: #007AFF;
      }
    }
    
    .textarea {
      height: 120rpx;
      resize: none;
    }
  }
}

.action-section {
  .create-btn {
    width: 100%;
    height: 96rpx;
    background-color: #007AFF;
    color: white;
    border: none;
    border-radius: 48rpx;
    font-size: 32rpx;
    font-weight: 500;
    margin-bottom: 40rpx;
    
    &:disabled {
      background-color: #ccc;
      color: #999;
    }
  }
  
  .divider {
    text-align: center;
    margin: 40rpx 0;
    position: relative;
    
    &::before {
      content: '';
      position: absolute;
      top: 50%;
      left: 0;
      right: 0;
      height: 1rpx;
      background-color: #eee;
    }
    
    text {
      background-color: #f5f5f5;
      padding: 0 20rpx;
      color: #999;
      font-size: 28rpx;
    }
  }
  
  .join-btn {
    width: 100%;
    height: 96rpx;
    background-color: transparent;
    color: #007AFF;
    border: 2rpx solid #007AFF;
    border-radius: 48rpx;
    font-size: 32rpx;
    font-weight: 500;
  }
}
</style> 